package com.example.wechat;

import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private Fragment setragment=new setragment();
    private  Fragment chatFragment=new chatFragment();
    private  Fragment discoverFragment = new discoverFragment();
    private  Fragment linmanFragment =new linmanFragment();

    private FragmentManager fragmentManager;
    private LinearLayout LinearLayout1,LinearLayout2,LinearLayout3,LinearLayout4;
    private TextView  textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout1 = findViewById(R.id.lot1);
        LinearLayout2=findViewById(R.id.lout2);
        LinearLayout3=findViewById(R.id.lout3);
        LinearLayout4=findViewById(R.id.lout4);
        textView=findViewById(R.id.textView0);
        LinearLayout1.setOnClickListener((View.OnClickListener) this);
        LinearLayout2.setOnClickListener((View.OnClickListener) this);
        LinearLayout3.setOnClickListener((View.OnClickListener) this);
        LinearLayout4.setOnClickListener((View.OnClickListener) this);
        initFragment();
    }



    private void initFragment(){
        fragmentManager = getSupportFragmentManager();
        androidx.fragment.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.add(R.id.content,setragment);
        transaction.add(R.id.content,chatFragment);
        transaction.add(R.id.content,discoverFragment);
        transaction.add(R.id.content,linmanFragment);
        hideFragment(transaction);
        transaction.show(setragment);
        transaction.commit();
    }
    private void hideFragment(androidx.fragment.app.FragmentTransaction transaction) {
        transaction.hide(setragment);
        transaction.hide(chatFragment);
        transaction.hide(discoverFragment);
        transaction.hide(linmanFragment);
    }




    private void showcolor(int i) {
        LinearLayout1.setBackgroundColor(Color.WHITE);
        LinearLayout2.setBackgroundColor(Color.WHITE);
        LinearLayout3.setBackgroundColor(Color.WHITE);
        LinearLayout4.setBackgroundColor(Color.WHITE);
        switch (i) {
            case 0:
                LinearLayout1.setBackgroundColor(Color.GREEN);
                textView.setText("聊天");
                break;
            case 1:
                LinearLayout2.setBackgroundColor(Color.GREEN);
                textView.setText("联系人");
                break;
            case 2:
                LinearLayout3.setBackgroundColor(Color.GREEN);
                textView.setText("朋友圈");
                break;
            case 3:
                LinearLayout4.setBackgroundColor(Color.GREEN);
                textView.setText("设置");
                break;
            default:
                break;

        }
    }
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.lot1:
                showfragment(0);
                showcolor(0);

                break;
            case R.id.lout2:
                showfragment(1);
                showcolor(1);
                break;
            case R.id.lout3:t:
                showfragment(2);
                showcolor(2);
                break;
            case R.id.lout4:
                showfragment(3);
                showcolor(3);
                break;
            default:
                break;

        }
    }

    private  void showfragment(int i){
        androidx.fragment.app.FragmentTransaction transaction = fragmentManager.beginTransaction();
        hideFragment(transaction);
        switch (i) {
            case 0:
                Log.d("setSelect","1");
                transaction.show(chatFragment);
                LinearLayout1.setBackgroundColor(Color.BLACK);
                break;
            case 1:
                transaction.show(linmanFragment);
                break;
            case 2:
                transaction.show(discoverFragment);
                break;
            case 3:
                transaction.show(setragment);
                break;
            default:
                break;
        }
        transaction.commit();
    }
}